package com.qianfeng.ls.dao.impl;

import com.qianfeng.ls.dao.IUserDao;
import com.qianfeng.ls.entity.UserEntity;
import org.hibernate.SessionFactory;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl implements IUserDao {

    @Autowired
    SessionFactory sessionFactory;

    /**
     * 登录方法
     * @param userEntity
     * @return
     */
    public UserEntity login(UserEntity userEntity){
        String sql = "select uid,uname,upass,uacount from t_user where uacount=? and upass=?";
        NativeQuery sqlQuery = sessionFactory.openSession().createSQLQuery(sql);
        sqlQuery.setParameter(1,userEntity.getUacount());
        sqlQuery.setParameter(2,userEntity.getUpass());

        List list = sqlQuery.list();

        if(null != list && list.size() > 0){
            Object[] o = (Object[]) list.get(0);
            UserEntity u = new UserEntity();
            u.setUid(Integer.parseInt(o[0].toString()));
            u.setUname(o[1].toString());
            u.setUpass(o[2].toString());
            u.setUacount(o[3].toString());

            return u;
        }

        return null;
    }

}
